﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Diagnostics;
using System.Xml;
using System.IO;
using System.Collections;
using Microsoft.SharePoint.Utilities;

namespace de.rwth.rz.campus.Mailer
{
    /// <remarks>
    /// 
    /// Copyright 2010 RWTH Aachen, CAMPUS Labs
    /// 
    /// Licensed under the Apache License, Version 2.0 (the "License");
    /// you may not use this file except in compliance with the License.
    /// You may obtain a copy of the License at
    ///
    /// http://www.apache.org/licenses/LICENSE-2.0
    ///
    /// Unless required by applicable law or agreed to in writing, software
    /// distributed under the License is distributed on an "AS IS" BASIS,
    /// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    /// See the License for the specific language governing permissions and
    /// limitations under the License.
    /// 
    /// </remarks>

    class DeployGlobalResources : SPJobDefinition
    {
        #region Fields
        [Persisted]
        private string sourcePath;
        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor used by the system.
        /// </summary>
        public DeployGlobalResources() : base() { }


        /// <summary>
        /// Contructor used to create an instance, which deploys the resource files.
        /// </summary>
        /// <param name="WebApp">The SPWebApplication which needs to be updated</param>
        public DeployGlobalResources(string jobName, SPWebApplication webApp, string featureName)
            : base(jobName, webApp, null, SPJobLockType.None)
        {
            // Detrmine the path to the feature's resource files
            sourcePath = string.Format("{0}\\FEATURES\\{1}\\",
                                        SPUtility.GetGenericSetupPath("Template"),
                                        featureName);
        }

        #endregion

        #region Job Handling
        /// <summary>
        /// Called by OWSTIMER when the job runs on a server. Overriden from SPJobDefinition.
        /// </summary>
        public override void Execute(Guid targetInstanceId)
        {
            try
            {
                SPWebApplication webApp = this.Parent as SPWebApplication;

                foreach (SPUrlZone zone in webApp.IisSettings.Keys)
                {
                    // The settings of the IIS application to update
                    SPIisSettings oSettings = webApp.IisSettings[zone];

                    // Determine the destination path
                    string destPath = Path.Combine(oSettings.Path.ToString(), "App_GlobalResources");
                    string[] filePaths = Directory.GetFiles(sourcePath, "*.resx");

                    // Copy the files
                    foreach (string filePath in filePaths)
                    {
                        string fileName = Path.GetFileName(filePath);
                        File.Copy(filePath, Path.Combine(destPath, fileName), true);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to copy global resources");
                Debug.WriteLine(ex);
                throw;
            }
        }
        #endregion
    }
}
